package www.wen.com.common.lib.extension

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.annotation.IdRes
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.navOptions
import com.orhanobut.logger.Logger
//import com.tencent.bugly.crashreport.CrashReport
import www.wen.com.common.R

fun AppCompatActivity.nav(
    @IdRes resId: Int,
    @Nullable args: Bundle?,
    pInclusive: Boolean? = false
) {
    navProxy()?.navigate(resId, args, navOptions {
        launchSingleTop = false
        anim {
            enter = R.anim.slide_in_right
            exit = R.anim.slide_out_left//Fixme 有exit动画时会出现闪屏问题，暂时没有找到解决方案
            popEnter = R.anim.slide_in_left
            popExit = R.anim.slide_out_right
        }
    })
}

fun Fragment.nav(@IdRes resId: Int, @Nullable args: Bundle? = null, withAnim: Boolean? = true) {
    try {
        nav().navigate(resId, args, navOptions {
            launchSingleTop = false
            if (withAnim == true) {
                anim {
                    enter = R.anim.slide_in_right
                    exit = R.anim.slide_out_left//Fixme 有exit动画时会出现闪屏问题，暂时没有找到解决方案
                    popEnter = R.anim.slide_in_left
                    popExit = R.anim.slide_out_right
                }
            }
        })
    } catch (e: Exception) {
        //可能是重复点击引起,https://www.jianshu.com/p/3e4a25e4d7aa
        Logger.e("nav:", e)
//        CrashReport.postCatchedException(e)
    }
}

fun Fragment.popProxy(@IdRes resId: Int, inclusive: Boolean = false) {
    try {
        nav().popBackStack(resId, inclusive)
    } catch (e: Exception) {
        Logger.e("popProxy:", e)
    }
}

fun AppCompatActivity.navProxy(): NavController? {
    var controller: NavController? = null
    supportFragmentManager.findFragmentById(R.id.host_fragment)?.apply {
        controller = www.wen.com.common.lib.fix.NavHostFragment.findNavController(this)
        if (controller == null) {
            controller = findNavController()
        }
    }
    return controller
}


private fun Fragment.nav(): NavController {
    return NavHostFragment.findNavController(this)
}

fun Fragment.navBack() {
    nav().popBackStack()
}

/**
 * 重置导航起始页面
 */
@SuppressLint("RestrictedApi")
fun FragmentActivity.resetStartDestination(
    nav: Int,
    startDes: Int,
    bundle: Bundle = Bundle()
) {

    if (supportFragmentManager.fragments[0] !is www.wen.com.common.lib.fix.NavHostFragment) return

    val fragments = supportFragmentManager.fragments[0].childFragmentManager.fragments
    fragments.forEach {
        supportFragmentManager.fragments[0].childFragmentManager.beginTransaction().remove(it)
            .commitAllowingStateLoss()
    }

    findNavController(R.id.host_fragment).apply {
        setGraph(navInflater.inflate(nav).apply {
            startDestination = startDes
        }, bundle)
    }
}